package com.g13.virtualpethospital.service.Implement;

import com.g13.virtualpethospital.entity.QuestionEntity;
import com.g13.virtualpethospital.entity.TestPaperBriefEntity;
import com.g13.virtualpethospital.entity.TestPaperEntity;
import com.g13.virtualpethospital.entity.UserEntity;
import com.g13.virtualpethospital.exceptions.TestPaperException;
import com.g13.virtualpethospital.service.TestPaperService;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title: TestPaperServiceImpl
 * @Author yyj1205
 * @Package com.g13.virtualpethospital.service.Implement
 * @Date 24/3/2024 23:14
 * @description: TestPaperService的一个实现
 */
@Service
public class TestPaperServiceImpl implements TestPaperService {
    @Autowired
    MongoTemplate mongoTemplate;
    @Override
    public void addTestPaper(TestPaperBriefEntity testPaperBrief) {
        testPaperBrief.setPaperId(null);
        mongoTemplate.insert(testPaperBrief);
    }

    @Override
    public void deleteTestPaper(String id) throws TestPaperException {
        TestPaperBriefEntity testPaperBrief = mongoTemplate.findById(id, TestPaperBriefEntity.class);
        if(testPaperBrief == null) throw new TestPaperException(TestPaperException.TestPaperErrorCode.FIND_NO_TEST);
        Query query = new Query(Criteria.where("_id").is(id));
        DeleteResult result = mongoTemplate.remove(query,TestPaperBriefEntity.class);
        if(result.getDeletedCount() == 0) throw new TestPaperException(TestPaperException.TestPaperErrorCode.DEL_FAIL);
    }

    @Override
    public void updateTestPaper(TestPaperBriefEntity testPaperBrief, String id) throws TestPaperException {
        TestPaperBriefEntity testPaperBrief1 = mongoTemplate.findById(id, TestPaperBriefEntity.class);
        if(testPaperBrief1 == null) throw new TestPaperException(TestPaperException.TestPaperErrorCode.FIND_NO_TEST);
        Query query = new Query(Criteria.where("_id").is(id));
        Update update = new Update();
        if(testPaperBrief.getHeadline() != null)
        update.set("headline", testPaperBrief.getHeadline());
        if (testPaperBrief.getQuestions() != null){
            for(String question: testPaperBrief1.getQuestions()){
                QuestionEntity questionEntity = mongoTemplate.findById(question, QuestionEntity.class);
                Update update1 = new Update();
                update1.set("usedCnt",questionEntity.getUsedCnt() - 1);
            }
            update.set("questions", testPaperBrief.getQuestions());
            for(String question: testPaperBrief.getQuestions()){
                QuestionEntity questionEntity = mongoTemplate.findById(question, QuestionEntity.class);
                Update update1 = new Update();
                update1.set("usedCnt",questionEntity.getUsedCnt() + 1);
            }
        }
//        if (testPaperBrief.getTotalScore() != null)
        update.set("totalScore", testPaperBrief.getTotalScore());
//        if (testPaperBrief.getTime() != null)
        update.set("time", testPaperBrief.getTime());
        if (testPaperBrief.getScores() != null)
        update.set("scores", testPaperBrief.getScores());
        UpdateResult result = mongoTemplate.upsert(query, update, TestPaperBriefEntity.class);
    }

    @Override
    public List<TestPaperBriefEntity> findTestPaperByHeadline(String headline) {
        if(headline == "" || headline == null) return findAllTestPaper();
        Query query = new Query(Criteria.where("headline").is(headline));
        return mongoTemplate.find(query, TestPaperBriefEntity.class);
    }

    @Override
    public List<TestPaperBriefEntity> findAllTestPaper() {

        return mongoTemplate.findAll(TestPaperBriefEntity.class);
    }

    @Override
    public List<TestPaperBriefEntity> findTestPaperById(String id) {
        TestPaperBriefEntity testPaperBrief = mongoTemplate.findById(id, TestPaperBriefEntity.class);
        List<TestPaperBriefEntity> paperBriefEntities = new ArrayList<>();
        if(testPaperBrief != null) paperBriefEntities.add(testPaperBrief);
        return paperBriefEntities;
    }


}
